package cn.yusei.teamTribe.controller.relation;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.MessageType;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.dao.relation.po.RelationRequestPO;
import cn.yusei.teamTribe.dao.user.UserDAO;
import cn.yusei.teamTribe.entity.Relation;
import cn.yusei.teamTribe.exception.RelationRequestNotExistsException;
import cn.yusei.teamTribe.service.MessageService;
import cn.yusei.teamTribe.service.relation.RelationRequestService;
import cn.yusei.teamTribe.service.relation.RelationService;
import cn.yusei.teamTribe.util.ResponseUtil;

@Controller
public class RelationController {

	@Autowired
	private RelationService relationService;
	@Autowired
	private RelationRequestService relationRequestService;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private MessageService messageService;
	
	@Value("${maxReasonLength}")
	private int maxReasonLength;
	
	/**
	 * 向一个用户发送好友申请
	 * 
	 * @param id1  发送请求的 id
	 * @param id2  接受请求的 id
	 * @param out
	 */
	@RequestMapping(value="/relation/sendRequest")
	public void sendRequest(
			@RequestAttribute("id") int id1,
			@RequestParam("id") int id2,
			@RequestParam(value="reason", defaultValue="") String reason,
			PrintWriter out)
	{
		if(id1 == id2 || id2 <= 0 || (id2 > id1 && userDAO.selectById(id2) == null))
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		if(reason.length() > maxReasonLength)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		long requestId = 0;
		try
		{
			requestId = relationRequestService.sendRequest(id1, id2, reason);
			messageService.addMessage(id2, MessageType.RELATION_REQUEST, requestId);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, ResponseCode.REQUEST_SUCCESSED);
	}
	
	@RequestMapping(value="/relation/getRequest")
	public void getRequest(@RequestAttribute("id") int id, @RequestParam("id") long requestId, PrintWriter out)
	{
		if(requestId <= 0)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		RelationRequestPO relationRequest = null;
		try
		{
			relationRequest = relationRequestService.getRequest(requestId);
		}
		catch (RelationRequestNotExistsException e)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("relationRequest", relationRequest);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	/**
	 * 对一个好友请求回应
	 * 
	 * @param id
	 * @param requestId  好友请求的 id
	 * @param result  1同意2拒绝
	 * @param reason 拒绝的理由，可选
	 * @param out
	 */
	@RequestMapping("/relation/sendResponse")
	public void sendResponse(
			@RequestAttribute("id") int id,
			@RequestParam("id") int requestId,
			@RequestParam("result") int result,
			@RequestParam(value="reason", defaultValue="") String reason,
			PrintWriter out)
	{
		if(requestId <= 0)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		if(result != 1 && result != 2)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		if(reason.length() > maxReasonLength)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int senderId = 0;
		try
		{
			senderId = relationRequestService.sendResponse(id, requestId, result, reason);
			messageService.addMessage(senderId, MessageType.RELATION_RESPONSE, requestId);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		int code = 0;
		if(result == 1)
		{
			try
			{
				code = relationService.add(requestId);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
				return;
			}
		}
		messageService.addMessage(id, MessageType.RELATION_RESPONSE, requestId);
		ResponseUtil.responseCode(out, code);
	}
	
	/**
	 * 删除和某人的好友关系
	 * 
	 * @param id1
	 * @param id2
	 * @param out
	 */
	@RequestMapping(value="/relation/delete")
	public void delete(@RequestAttribute("id") int id1, @RequestParam("id") int id2, PrintWriter out)
	{
		if(id2 <= 0)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		Relation friendRelation = new Relation();
		int minId = Math.min(id1, id2);
		int maxId = Math.max(id1, id2);
		friendRelation.setId1(minId);
		friendRelation.setId2(maxId);
		int code = 0;
		try
		{
			code = relationService.delete(friendRelation);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
	
	/**
	 * 获取所有好友的 id
	 * 
	 * @param id
	 * @param out
	 */
	@RequestMapping(value="/relation/get")
	public void get(@RequestAttribute("id") int id, PrintWriter out)
	{
		List<Integer> ids = null;
		try
		{
			ids = relationService.get(id);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("relation", ids);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
}
